package edu.gyc.objecthomework;

/**
 File name: YearsToOvertakePhase3.java
 
 This program is the third step towards a final solution.
   It includes comments that outline the entire solution,
   reads in two species,
   determines which has the lower initial population,
   prints out the name, population and growth rate for the
       species with the lower population,
   followed by the name, population and growth rate for the
       species with the higher population,
   then calculates (and displays) the number of years it takes
   the lower species to overtake the other one.
 
 When this program is complete it will do the following and have the
   postconditions stated below:
 
   Read in the names, populations and growth rates for two
   species and calculate the number of years it will take the one
   with the lower population to overtake the other (or a message that
   it will never have a higher population).
 
 Preconditions: none.
 
 Postconditions:
   Displays either the number of years it takes for the species with the
   lower population to have a population greater than the other,
   or a message that it will not exceed the other
   species' population within ten years.
 
 Author: Lew Rakocy
 email address: Lrakocy@devrycols.edu
 Date: <2/20/99>
 Changes: 08/26/2000 Changed to newer version of Species.
          02/23/2003 Made comments like text examples.
                     Years to overtake was one too many - changed
                     postdecrement (years--) to predecrement (--years).
*/
public class YearsToOvertakePhase3
{
     public static void main(String[] args)
     {
       Species s1 = new Species();
       Species s2 = new Species();
       Species lower = new Species();  // has lower initial population
       Species higher = new Species(); // has higher initial population

        // Read in two species
        s1.readInput();

        // Special condition detection:
        // What if they have the same initial populations?

        // Determine which has the lower initial population
        
        if(s1.getPopulation() < s2.getPopulation())
        {
            lower.set(s1.getName(), s1.getPopulation(), s1.getGrowthRate());
            higher.set(s2.getName(), s2.getPopulation(), s2.getGrowthRate());
        }
        else
        {
            lower.set(s2.getName(), s2.getPopulation(), s2.getGrowthRate());
            higher.set(s1.getName(), s1.getPopulation(), s1.getGrowthRate());
        }

        // Test: can be deleted after this third stage program works
        
        System.out.println("The species that has the lower initial "
               + "population is");
        lower.writeOutput();
        System.out.println("The species with the higher population is");
        higher.writeOutput();
        
        // End of test

        // Find number of years it takes for the species with lower
        // initial population to overtake the other species.
        
        int years = 1;
        int nextPopulationLower = lower.getPopulation();
        int nextPopulationHigher = higher.getPopulation();
        while(
            nextPopulationLower <= nextPopulationHigher && years <= 10)
        {
            nextPopulationLower = lower.projectedPopulation(years);
            
            // Test: delete statement after this third stage program works
            
            System.out.println("After " + years + " years, Lower pop = "
                   + nextPopulationLower);
            
            nextPopulationHigher = higher.projectedPopulation(years);
            
            // Test: delete statement after this third stage program works
            
            System.out.println("After " + years + " years, Higher pop = "
                   + nextPopulationHigher);
            
            years++; // WARNING: exit value of years will be 1 too many
        }
        
        // Adjust years to correct value
        // (it is 1 too many on exit from while-loop)
        
        System.out.println("years = " + (--years));

        // Be careful when years = 10:
        // "lower" may or may not have overtaken "higher"
        // so you must do a final check:
        //   if(years = 10 && popSmaller <= popLarger)
        //      print "lower never overtakes higher"
        //   else print "lower overtakes higher after x years"
    }
 }

